home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / srcuc.zip / FUTURE.C < prev    next >
C/C++ Source or Header  |  1989-09-20  |  13KB  |  400 lines

  1. /* -*-C-*-
  2.  
  3. $Header: future.c,v 9.27 89/09/20 23:08:34 GMT cph Exp $
  4.  
  5. Copyright (c) 1987, 1988, 1989 Massachusetts Institute of Technology
  6.  
  7. This material was developed by the Scheme project at the Massachusetts
  8. Institute of Technology, Department of Electrical Engineering and
  9. Computer Science.  Permission to copy this software, to redistribute
  10. it, and to use it for any purpose is granted, subject to the following
  11. restrictions and understandings.
  12.  
  13. 1. Any copy made of this software must include this copyright notice
  14. in full.
  15.  
  16. 2. Users of this software agree to make their best efforts (a) to
  17. return to the MIT Scheme project any improvements or extensions that
  18. they make, so that these may be included in future releases; and (b)
  19. to inform MIT of noteworthy uses of this software.
  20.  
  21. 3. All materials developed as a consequence of the use of this
  22. software shall duly acknowledge such use, in accordance with the usual
  23. standards of acknowledging credit in academic research.
  24.  
  25. 4. MIT has made no warrantee or representation that the operation of
  26. this software will be error-free, and MIT is under no obligation to
  27. provide any services, by way of maintenance, update, or otherwise.
  28.  
  29. 5. In conjunction with products arising from the use of this material,
  30. there shall be no use of the name of the Massachusetts Institute of
  31. Technology nor of any adaptation thereof in any advertising,
  32. promotional, or sales literature without prior written consent from
  33. MIT in each case. */
  34.  
  35. /* Support code for futures */
  36.  
  37. #include "scheme.h"
  38. #include "prims.h"
  39. #include "locks.h"
  40.  
  41. #ifndef COMPILE_FUTURES
  42. #include "Error: future.c is useless without COMPILE_FUTURES"
  43. #endif
  44.  
  45. /* This is how we support future numbering for external metering */
  46. #ifndef New_Future_Number
  47. #define New_Future_Number() SHARP_F
  48. #else
  49. SCHEME_OBJECT Get_New_Future_Number ();
  50. #endif
  51.  
  52. /*
  53.  
  54. A future is a VECTOR starting with <determined?>, <locked?> and
  55. <waiting queue / value>,
  56.  
  57. where <determined?> is #!false if no value is known yet,
  58.                        #!true if value is known and future can vanish at GC,
  59.                        otherwise value is known, but keep the slot
  60.  
  61. and where <locked> is #!true if someone wants slot kept for a time.
  62.  
  63. */
  64.  
  65. DEFINE_PRIMITIVE ("TOUCH", Prim_touch, 1, 1, 0)
  66. {
  67.   SCHEME_OBJECT result;
  68.   PRIMITIVE_HEADER (1);
  69.   TOUCH_IN_PRIMITIVE ((ARG_REF (1)), result);
  70.   PRIMITIVE_RETURN (result);
  71. }
  72.  
  73. DEFINE_PRIMITIVE ("FUTURE?", Prim_future_p, 1, 1, 0)
  74. {
  75.   PRIMITIVE_HEADER (1);
  76.   PRIMITIVE_RETURN (BOOLEAN_TO_OBJECT (FUTURE_P (ARG_REF (1))));
  77. }
  78.  
  79. /* Utility setting routine for use by the various test and set if
  80.    equal operators.
  81. */
  82.  
  83. long
  84. Set_If_Equal(Base, Offset, New, Wanted)
  85.      SCHEME_OBJECT Base, Wanted, New;
  86.      long Offset;
  87. {
  88.   Lock_Handle lock;
  89.   SCHEME_OBJECT Old_Value, Desired, Remember_Value;
  90.   long success;
  91.  
  92.   TOUCH_IN_PRIMITIVE(Wanted, Desired);
  93. Try_Again:
  94.   Remember_Value = MEMORY_REF (Base, Offset);
  95.   TOUCH_IN_PRIMITIVE(Remember_Value, Old_Value);
  96.   lock = Lock_Cell(MEMORY_LOC (Base, Offset));
  97.   if (Remember_Value != FAST_MEMORY_REF (Base, Offset))
  98.   {
  99.     Unlock_Cell(lock);
  100.     goto Try_Again;
  101.   }
  102.   if (Old_Value == Desired)
  103.   {
  104.     Do_Store_No_Lock(MEMORY_LOC (Base, Offset), New);
  105.     success = true;
  106.   }
  107.   else
  108.   {
  109.     success = false;
  110.   }
  111.   Unlock_Cell(lock);
  112.   return success;
  113. }
  114.  
  115. DEFINE_PRIMITIVE ("SET-CAR-IF-EQ?!", Prim_set_car_if_eq, 3, 3,
  116.   "Replace the car of PAIR with NEW-VALUE iff it contains OLD-VALUE.\n\
  117. Return PAIR if so, otherwise return '().")
  118. {
  119.   PRIMITIVE_HEADER (3);
  120.   CHECK_ARG (1, PAIR_P);
  121.   {
  122.     fast SCHEME_OBJECT pair = (ARG_REF (1));
  123.     if (Set_If_Equal (pair, CONS_CAR, (ARG_REF (2)), (ARG_REF (3))))
  124.       PRIMITIVE_RETURN (pair);
  125.   }
  126.   PRIMITIVE_RETURN (EMPTY_LIST);
  127. }
  128.  
  129. DEFINE_PRIMITIVE ("SET-CDR-IF-EQ?!", Prim_set_cdr_if_eq, 3, 3,
  130.   "Replace the cdr of PAIR with NEW-VALUE iff it contains OLD-VALUE.\n\
  131. Return PAIR if so, otherwise return '().")
  132. {
  133.   PRIMITIVE_HEADER (3);
  134.   CHECK_ARG (1, PAIR_P);
  135.   {
  136.     fast SCHEME_OBJECT pair = (ARG_REF (1));
  137.     if (Set_If_Equal (pair, CONS_CDR, (ARG_REF (2)), (ARG_REF (3))))
  138.       PRIMITIVE_RETURN (pair);
  139.   }
  140.   PRIMITIVE_RETURN (EMPTY_LIST);
  141. }
  142.  
  143. /* (VECTOR-SET-IF-EQ?! <Vector> <Offset> <New Value> <Old Value>)
  144.    Replaces the <Offset>th element of <Vector> with <New Value> if it used
  145.    to contain <Old Value>.  The value returned is either <Vector> (if
  146.    the modification takes place) or '() if it does not.
  147. */
  148. DEFINE_PRIMITIVE ("VECTOR-SET-IF-EQ?!", Prim_vector_set_if_eq, 4, 4,
  149.   "Replace VECTOR's INDEX'th element with NEW-VALUE iff it contains OLD-VALUE.\n\
  150. Return VECTOR if so, otherwise return '().")
  151. {
  152.   PRIMITIVE_HEADER (4);
  153.   CHECK_ARG (1, VECTOR_P);
  154.   {
  155.     fast SCHEME_OBJECT vector = (ARG_REF (1));
  156.     if (Set_If_Equal
  157.     (vector,
  158.      ((arg_index_integer (2, (VECTOR_LENGTH (vector)))) + 1),
  159.      (ARG_REF (3)),
  160.      (ARG_REF (4))))
  161.       PRIMITIVE_RETURN (vector);
  162.   }
  163.   PRIMITIVE_RETURN (EMPTY_LIST);
  164. }
  165.  
  166. DEFINE_PRIMITIVE ("SET-CXR-IF-EQ?!", Prim_set_cxr_if_eq, 4, 4,
  167.   "Replace HUNK3's INDEX'th element with NEW-VALUE iff it contains OLD-VALUE.\n\
  168. Return HUNK3 if so, otherwise return '().")
  169. {
  170.   PRIMITIVE_HEADER (4);
  171.   CHECK_ARG (1, HUNK3_P);
  172.   {
  173.     fast SCHEME_OBJECT hunk3 = (ARG_REF (1));
  174.     if (Set_If_Equal
  175.     (hunk3,
  176.      ((arg_index_integer (2, 3)) + 1),
  177.      (ARG_REF (3)),
  178.      (ARG_REF (4))))
  179.       PRIMITIVE_RETURN (hunk3);
  180.   }
  181.   PRIMITIVE_RETURN (EMPTY_LIST);
  182. }
  183.  
  184. DEFINE_PRIMITIVE ("FUTURE-SIZE", Prim_future_size, 1, 1,
  185.   "Return the number of elements in FUTURE.\n\
  186. This is similar to SYSTEM-VECTOR-SIZE,\n\
  187. but works only on futures and doesn't touch them.")
  188. {
  189.   PRIMITIVE_HEADER (1)
  190.   CHECK_ARG (1, FUTURE_P);
  191.   PRIMITIVE_RETURN (LONG_TO_UNSIGNED_FIXNUM (VECTOR_LENGTH (ARG_REF (1))));
  192. }
  193.  
  194. DEFINE_PRIMITIVE ("FUTURE-REF", Prim_future_ref, 2, 2,
  195.   "Return FUTURE's INDEX'th element.\n\
  196. This is similar to SYSTEM-VECTOR-REF,\n\
  197. but works only on futures and doesn't touch them.")
  198. {
  199.   PRIMITIVE_HEADER (2);
  200.   CHECK_ARG (1, FUTURE_P);
  201.   {
  202.     fast SCHEME_OBJECT future = (ARG_REF (1));
  203.     PRIMITIVE_RETURN
  204.       (VECTOR_REF
  205.        (future, (arg_index_integer (2, (VECTOR_LENGTH (future))))));
  206.   }
  207. }
  208.  
  209. DEFINE_PRIMITIVE ("FUTURE-SET!", Prim_future_set, 3, 3,
  210.   "Modify FUTURE's INDEX'th element to be VALUE.\n\
  211. This is similar to SYSTEM-VECTOR-SET!,\n\
  212. but works only on futures and doesn't touch them.")
  213. {
  214.   PRIMITIVE_HEADER (3);
  215.   CHECK_ARG (1, FUTURE_P);
  216.   {
  217.     fast SCHEME_OBJECT future = (ARG_REF (1));
  218.     fast long index = (arg_index_integer (2, (VECTOR_LENGTH (future))));
  219.     fast SCHEME_OBJECT result = (VECTOR_REF (future, index));
  220.     VECTOR_SET (future, index, (ARG_REF (3)));
  221.     PRIMITIVE_RETURN (result);
  222.   }
  223. }
  224.  
  225. DEFINE_PRIMITIVE ("LOCK-FUTURE!", Prim_lock_future, 1, 1,
  226.   "Set the lock flag on FUTURE.\n\
  227. This flag prevents FUTURE from being spliced out by the garbage collector.\n\
  228. If FUTURE is not a future, return #F immediately,\n\
  229. otherwise return #T after the lock has been set.\n\
  230. Will wait as long as necessary for the lock to be set.")
  231. {
  232.   PRIMITIVE_HEADER (1);
  233.   {
  234.     fast SCHEME_OBJECT future = (ARG_REF (1));
  235.     if (! (FUTURE_P (future)))
  236.       PRIMITIVE_RETURN (SHARP_F);
  237.     while (1)
  238.       {
  239.     if (INTERRUPT_PENDING_P (INT_Mask))
  240.       signal_interrupt_from_primitive ();
  241.     {
  242.       fast SCHEME_OBJECT lock;
  243.       SWAP_POINTERS ((MEMORY_LOC (future, FUTURE_LOCK)), SHARP_T, lock);
  244.       if (lock == SHARP_F)
  245.         PRIMITIVE_RETURN (SHARP_T);
  246.     }
  247.     Sleep (CONTENTION_DELAY);
  248.       }
  249.   }
  250. }
  251.  
  252. DEFINE_PRIMITIVE ("UNLOCK-FUTURE!", Prim_unlock_future, 1, 1,
  253.   "Clear the lock flag on FUTURE.\n\
  254. If FUTURE is not a future, return #F immediately,\n\
  255. otherwise return #T after the lock has been cleared.")
  256. {
  257.   PRIMITIVE_HEADER (1);
  258.   {
  259.     fast SCHEME_OBJECT future = (ARG_REF (1));
  260.     if (! (FUTURE_P (future)))
  261.       PRIMITIVE_RETURN (SHARP_F);
  262.     if (! (Future_Is_Locked (future)))
  263.       error_wrong_type_arg (1);
  264.     MEMORY_SET (future, FUTURE_LOCK, SHARP_F);
  265.     PRIMITIVE_RETURN (SHARP_T);
  266.   }
  267. }
  268.  
  269. DEFINE_PRIMITIVE ("FUTURE->VECTOR", Prim_future_to_vector, 1, 1,
  270.   "Return a newly-allocated vector containing FUTURE's elements.
  271. If FUTURE is not a future, return #F instead.")
  272. {
  273.   PRIMITIVE_HEADER (1);
  274.   {
  275.     SCHEME_OBJECT future = (ARG_REF (1));
  276.     if (! (FUTURE_P (future)))
  277.       PRIMITIVE_RETURN (SHARP_F);
  278.     {
  279.       long length = (VECTOR_LENGTH (future));
  280.       fast SCHEME_OBJECT * scan_source = (MEMORY_LOC (future, 1));
  281.       fast SCHEME_OBJECT * end_source = (scan_source + length);
  282.       SCHEME_OBJECT result =
  283.     (allocate_marked_vector (TC_VECTOR, length, true));
  284.       fast SCHEME_OBJECT * scan_result = (MEMORY_LOC (result, 1));
  285.       while (scan_source < end_source)
  286.     (*scan_result++) = (MEMORY_FETCH (*scan_source++));
  287.       PRIMITIVE_RETURN (result);
  288.     }
  289.   }
  290. }
  291.  
  292. DEFINE_PRIMITIVE ("NON-TOUCHING-EQ?", Prim_future_eq, 2, 2, 0)
  293. {
  294.   PRIMITIVE_HEADER (2);
  295.   PRIMITIVE_RETURN (BOOLEAN_TO_OBJECT ((ARG_REF (1)) == (ARG_REF (2))));
  296. }
  297.  
  298. /* MAKE-INITIAL-PROCESS is called to create a small stacklet which
  299.  * will just call the specified thunk and then end the computation
  300.  */
  301.  
  302. DEFINE_PRIMITIVE ("MAKE-INITIAL-PROCESS", Prim_make_initial_process, 1, 1, 0)
  303. {
  304.   SCHEME_OBJECT Result;
  305.   long Useful_Length;
  306.   PRIMITIVE_HEADER (1);
  307.  
  308.   Result = MAKE_POINTER_OBJECT (TC_CONTROL_POINT, Free);
  309.   Useful_Length = (3 * CONTINUATION_SIZE) + STACK_ENV_EXTRA_SLOTS + 1;
  310.  
  311. #ifdef USE_STACKLETS
  312.  
  313.   {
  314.     long Allocated_Length, Waste_Length;
  315.  
  316.     Allocated_Length = (Useful_Length + STACKLET_SLACK + STACKLET_HEADER_SIZE);
  317.     if (Allocated_Length < Default_Stacklet_Size)
  318.     {
  319.       Allocated_Length = Default_Stacklet_Size;
  320.       Waste_Length = ((Allocated_Length + 1) -
  321.               (Useful_Length + STACKLET_HEADER_SIZE));
  322.     }
  323.     else
  324.     {
  325.       Waste_Length = (STACKLET_SLACK + 1);
  326.     }
  327.     Primitive_GC_If_Needed(Allocated_Length + 1);
  328.     Free[STACKLET_LENGTH] =
  329.       MAKE_POINTER_OBJECT (TC_MANIFEST_VECTOR, Allocated_Length);
  330.     Free[STACKLET_REUSE_FLAG] = SHARP_T;
  331.     Free[STACKLET_UNUSED_LENGTH] =
  332.       MAKE_OBJECT (TC_MANIFEST_NM_VECTOR, Waste_Length);
  333.     Free += (Allocated_Length + 1) - Useful_Length;
  334.   }
  335.  
  336. #else /* not USE_STACKLETS */
  337.  
  338.   Free[STACKLET_LENGTH] =
  339.     MAKE_OBJECT (TC_MANIFEST_VECTOR, Useful_Length + STACKLET_HEADER_SIZE - 1);
  340.   Free[STACKLET_REUSE_FLAG] = SHARP_F;
  341.   Free[STACKLET_UNUSED_LENGTH] = MAKE_OBJECT (TC_MANIFEST_NM_VECTOR, 0);
  342.   Free += STACKLET_HEADER_SIZE;
  343.  
  344. #endif /* USE_STACKLETS */
  345.  
  346.   Free[CONTINUATION_EXPRESSION] = LONG_TO_FIXNUM(FETCH_INTERRUPT_MASK());
  347.   Free[CONTINUATION_RETURN_CODE] =
  348.     MAKE_OBJECT (TC_RETURN_CODE, RC_RESTORE_INT_MASK);
  349.   Free += CONTINUATION_SIZE;
  350.   Free[CONTINUATION_EXPRESSION] = SHARP_F;
  351.   Free[CONTINUATION_RETURN_CODE] =
  352.     MAKE_OBJECT (TC_RETURN_CODE, RC_INTERNAL_APPLY);
  353.   Free += CONTINUATION_SIZE;
  354.   *Free++ = STACK_FRAME_HEADER;
  355.   *Free++ = (ARG_REF (1));
  356.   Free[CONTINUATION_EXPRESSION] = (ARG_REF (1)); /* For testing & debugging */
  357.   Free[CONTINUATION_RETURN_CODE] =
  358.     MAKE_OBJECT (TC_RETURN_CODE, RC_END_OF_COMPUTATION);
  359.   Free += CONTINUATION_SIZE;
  360.   PRIMITIVE_RETURN (Result);
  361. }
  362.  
  363. /*
  364.   Absolutely the cheapest future we can make.  This includes
  365.   the I/O stuff and whatnot.  Notice that the name is required.
  366.  
  367.   (make-cheap-future orig-code user-proc name)
  368.  
  369. */
  370.  
  371. DEFINE_PRIMITIVE ("MAKE-CHEAP-FUTURE", Prim_make_cheap_future, 3, 3, 0)
  372. {
  373.   PRIMITIVE_HEADER (3);
  374.   {
  375.     fast SCHEME_OBJECT future = (allocate_marked_vector (TC_FUTURE, 10, true));
  376.     FAST_MEMORY_SET (future, FUTURE_IS_DETERMINED, SHARP_F);
  377.     FAST_MEMORY_SET (future, FUTURE_LOCK, SHARP_F);
  378.     FAST_MEMORY_SET (future, FUTURE_QUEUE, (cons (EMPTY_LIST, EMPTY_LIST)));
  379.     FAST_MEMORY_SET (future, FUTURE_PROCESS, (ARG_REF (1)));
  380.     FAST_MEMORY_SET (future, FUTURE_STATUS, SHARP_T);
  381.     FAST_MEMORY_SET (future, FUTURE_ORIG_CODE, (ARG_REF (2)));
  382.     /* Put the I/O system stuff here. */
  383.     FAST_MEMORY_SET
  384.       (future,
  385.        FUTURE_PRIVATE,
  386.        (make_vector
  387.     (1,
  388.      (hunk3_cons
  389.       (SHARP_F,
  390.        (ARG_REF (3)),
  391.        (cons ((LONG_TO_UNSIGNED_FIXNUM (0)),
  392.           (char_pointer_to_string ("")))))),
  393.      true)));
  394.     FAST_MEMORY_SET (future, FUTURE_WAITING_ON, EMPTY_LIST);
  395.     FAST_MEMORY_SET (future, FUTURE_METERING, (New_Future_Number ()));
  396.     FAST_MEMORY_SET (future, FUTURE_USER, SHARP_F);
  397.     PRIMITIVE_RETURN (future);
  398.   }
  399. }
  400.